package com.geek.framework.study.config.netflix;

import com.netflix.config.PollResult;
import com.netflix.config.PolledConfigurationSource;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 简单轮询配置源
 */
public class SimplePolledConfigurationSource implements PolledConfigurationSource {

    public static final String KEY = "currentTime";

    private AtomicInteger versions = new AtomicInteger(1);

    @Override
    public PollResult poll(boolean initial, Object checkPoint) throws Exception {
        if (initial) {
            return init();
        }else{
            Map<String, Object> addMap = new HashMap<>();

            HashMap<String, Object> updated = new HashMap<>();
            updated.put("currentTime", System.currentTimeMillis());

            Map<String, Object> deleted = new HashMap<>();
            return PollResult.createIncremental(addMap, updated, deleted, versions.getAndIncrement());
        }
    }

    private PollResult init() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("currentTime", System.currentTimeMillis());
        return PollResult.createFull(map);
    }
}
